/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.re.service.impl;

import me.zhengjie.re.domain.Reviews;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.re.repository.ReviewsRepository;
import me.zhengjie.re.service.ReviewsService;
import me.zhengjie.re.service.dto.ReviewsDto;
import me.zhengjie.re.service.dto.ReviewsQueryCriteria;
import me.zhengjie.re.service.mapstruct.ReviewsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yun
* @date 2024-02-20
**/
@Service
@RequiredArgsConstructor
public class ReviewsServiceImpl implements ReviewsService {

    private final ReviewsRepository reviewsRepository;
    private final ReviewsMapper reviewsMapper;

    @Override
    public PageResult<ReviewsDto> queryAll(ReviewsQueryCriteria criteria, Pageable pageable){
        Page<Reviews> page = reviewsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reviewsMapper::toDto));
    }

    @Override
    public List<ReviewsDto> queryAll(ReviewsQueryCriteria criteria){
        return reviewsMapper.toDto(reviewsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReviewsDto findById(Integer reId) {
        Reviews reviews = reviewsRepository.findById(reId).orElseGet(Reviews::new);
        ValidationUtil.isNull(reviews.getReId(),"Reviews","reId",reId);
        return reviewsMapper.toDto(reviews);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Reviews resources) {
        resources.setReCode("已发布");
        resources.setReData(new Timestamp(System.currentTimeMillis()));
        reviewsRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Reviews resources) {
        Reviews reviews = reviewsRepository.findById(resources.getReId()).orElseGet(Reviews::new);
        ValidationUtil.isNull( reviews.getReId(),"Reviews","id",resources.getReId());
        reviews.copy(resources);
        reviewsRepository.save(reviews);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer reId : ids) {
            reviewsRepository.deleteById(reId);
        }
    }

    @Override
    public void download(List<ReviewsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReviewsDto reviews : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("选择宝贝", reviews.getReBaby());
            map.put("评价等级", reviews.getReClass());
            map.put("评价人", reviews.getReUser());
            map.put("用户头像", reviews.getReUserImg());
            map.put("评论内容", reviews.getReContent());
            map.put("宝贝图片", reviews.getReBabyImg());
            map.put("联系方式", reviews.getRePhone());
            map.put("创建时间", reviews.getReData());
            map.put("状态", reviews.getReCode());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
